unit macros;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TMacrosForm = class(TForm)
    Macros: TListBox;
    DB: TButton;
    EB: TButton;
    RB: TButton;
    NB: TButton;
    RNB: TButton;
    procedure DBClick(Sender: TObject);
    procedure EBClick(Sender: TObject);
    procedure NBClick(Sender: TObject);
    procedure RNBClick(Sender: TObject);
    procedure RBClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    run : boolean;
  end;

var
  MacrosForm: TMacrosForm;

implementation

uses main, Edit;

{$R *.dfm}

procedure TMacrosForm.DBClick(Sender: TObject);
var i,j:integer;
begin
 if (Macros.ItemIndex > -1) and (Application.MessageBox('Do you want to delete macro ?','Delete macro',mb_iconquestion+mb_yesno) = idyes) then
 begin
   i := integer(Macros.Items.Objects[Macros.ItemIndex]);
   for j := i to high(Macro)-1 do Macro[i] := Macro[i+1];
   setlength(Macro, high(Macro));
   Macros.Items.Delete(Macros.ItemIndex);
 end;
end;

procedure TMacrosForm.EBClick(Sender: TObject);
var i:integer;
begin
 if Macros.ItemIndex > -1 then
 begin
  i := integer(Macros.Items.Objects[Macros.ItemIndex]);
  EditForm.Editor.Text :=Macro[i].text;
  if EditForm.showmodal = mrok then
  begin
   Macro[i].text := EditForm.Editor.Text;
   if (length(Macro[i].text) < 2) or (copy(Macro[i].text,length(Macro[i].text)-1,2) <> #13#10) then
    Macro[i].text := Macro[i].text + #13#10;
  end;
 end;
end;

procedure TMacrosForm.NBClick(Sender: TObject);
var n : string;
    i : integer;
begin
 n := 'NewMacro';
 if InputQuery('New Macro','Macro name:',n) and (n <> '') and (Macros.Items.IndexOf(n)=-1) then
 begin
  EditForm.Editor.Text := '';
  if EditForm.showmodal = mrok then
  begin
   i := high(Macro) + 1;
   setlength(Macro, i+1);
   Macro[i].name := n;
   Macro[i].text := EditForm.Editor.Text;
   if (length(Macro[i].text) < 2) or (copy(Macro[i].text,length(Macro[i].text)-1,2) <> #13#10) then
    Macro[i].text := Macro[i].text + #13#10;
   Macros.AddItem(n,TObject(i))
  end;
 end;
end;

procedure TMacrosForm.RNBClick(Sender: TObject);
var n : string;
    i : integer;
begin
 if Macros.ItemIndex > -1 then
 begin
  i := integer(Macros.Items.Objects[Macros.ItemIndex]);
  n := Macro[i].name;
  if InputQuery('Rename Macro','New macro name:',n) and (n <> '')
  and (Macros.Items.IndexOf(n)=-1) then
  Macro[i].name := n;
  Macros.Items[Macros.ItemIndex] := n;
 end;
end;

procedure TMacrosForm.RBClick(Sender: TObject);
var i : integer;
begin
 if run then
 begin
  RB.Caption := 'Run';
  run := false;
  NB.Enabled := true;
  RNB.Enabled := true;
  DB.Enabled := true;
  EB.Enabled := true;
  Mainform.F.stop := true;
 end
 else
 begin
  run := true;
  RB.Caption := 'Stop';
  NB.Enabled := false;
  RNB.Enabled := false;
  DB.Enabled := false;
  EB.Enabled := false;
  try
   i := integer(MacrosForm.Macros.Items.Objects[MacrosForm.Macros.ItemIndex]);
   MainForm.F.ComputeStr(Macro[i].text);
  except
   on E:Exception do
   begin
     Application.MessageBox(pchar(E.Message),'Error',mb_iconstop);
     EditForm.Editor.Text :=Macro[i].text;
     Editform.ActiveControl := Editform.Editor;
     Editform.Editor.SelStart := MainForm.F.ErrStrPos-1;
     if EditForm.showmodal = mrok then
     begin
       Macro[i].text := EditForm.Editor.Text;
       if (length(Macro[i].text) < 2) or (copy(Macro[i].text,length(Macro[i].text)-1,2) <> #13#10) then
       Macro[i].text := Macro[i].text + #13#10;
     end;
   end;
  end;
  RB.Caption := 'Run';
  run := false;
  NB.Enabled := true;
  RNB.Enabled := true;
  DB.Enabled := true;
  EB.Enabled := true;
 end;
end;

procedure TMacrosForm.FormShow(Sender: TObject);
begin
 run := false;
end;

end.
